namespace System.Linq.Expressions
{
    public class SubstitutionVisitor : ExpressionVisitorEx
    {
        private readonly LambdaExpression f_Substitution;
        private ParameterExpression f_Parameter;

        public SubstitutionVisitor(LambdaExpression Substitution)
        {
            f_Substitution = Substitution;
            f_Parameter = Substitution.Parameters.First();
        }

        protected override Expression VisitParameter(ParameterExpression p)
        {
            return p.Name == f_Parameter.Name && p.Type == f_Parameter.Type
                ? f_Substitution.Body
                : base.VisitParameter(p);
        }
    }
}